/* brief Store the buffers used for storing vertices and vertices indexes on GPU side
 
	Basically, an IBO store an Id wich can be used to find the corresponding vertex in the VBO Buffer array
	
	Thus, an IBO is very usefull as it avoid multiple vertex to be added multiples times. For instance, if we wanted to draw a rectangle with 2 triangles,
	we would have need 6 vertices. With an IBO, We can store only four .
	
	An IBO must containt short data (16 bit)

	VBO stand for "Vertex buffer object" and is used to store data on the GPU Side.

	The goal here is to use only one VBO wich will be used to store all the vertex's attributes,
	such as position(P), normal(N), color(C), texture coordinate/uv (U)

	The datas attributes will be interleave, so the VBO will store attribute this way :
	PNCUPNCUPNCU
	ATI recommand a vertex attribute to be 32-bytes multiple (32, 64,128), so, I should not hesitate
	to add blank float in my VBO. A float is 4 bytes.

	VBO Workflow : All Meshes write their vertices in the VBO, wich can be accessed by every class as Vbo is a static one.
	Please, remember that there's actually 2 IBO and VBO.
	One is used for writing operation, And the other for display. Thus, If we need to Edit the buffers datas (by inserting, deleting or modifying vertices),
	we must write in the writing buffers. Once writing is done, the developper must call the swap method to fill the displaying VBO and IBO.
	This is double buffering, and is used to avoid desync. If you write on the current rendering VBO, it may impact the rendering process
 */

 BufferObject = function ()
 {
 
 }
 
 
 BufferObject.s_WritingVBO;
 BufferObject.s_DisplayingVBO;
 
 BufferObject.s_WritingIBO;
 BufferObject.s_DisplayingIBO;
 
 
BufferObject.InitVBO= function()
{
	BufferObject.s_WritingVBO 		= 	gl.createBuffer();
	BufferObject.s_DisplayingVBO	=	gl.createBuffer();
}


BufferObject.InitIBO= function()
{
	BufferObject.s_WritingIBO 		= 	gl.createBuffer();
	BufferObject.s_DisplayingIBO	=	gl.createBuffer();
}


BufferObject.InitBuffers= function()
{
	BufferObject.InitVBO();
	BufferObject.InitIBO();
}

BufferObject.BindVBO= function()
{
	gl.bindBuffer(gl.ARRAY_BUFFER, BufferObject.s_DisplayingVBO);
}

BufferObject.BindIBO= function()
{
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, BufferObject.s_DisplayingIBO);
}

BufferObject.Swap= function()
{
	this.tempValueVBO = BufferObject.s_WritingVBO;
	this.tempValueIBO = BufferObject.s_WritingIBO;

	BufferObject.s_WritingVBO        =   BufferObject.s_DisplayingVBO;
	BufferObject.s_DisplayingVBO     =   this.tempValueVBO;

	BufferObject.s_WritingIBO        =   BufferObject.s_DisplayingIBO;
	BufferObject.s_DisplayingIBO     =   this.tempValueIBO;
}

BufferObject.FeedVBO= function()
{ 
	gl.bindBuffer(gl.ARRAY_BUFFER, BufferObject.s_WritingVBO);
	gl.bufferData(gl.ARRAY_BUFFER,	new Float32Array(Vertex.ToArray()), gl.STATIC_DRAW);
}

BufferObject.FeedIBO= function()
{
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, BufferObject.s_WritingIBO);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 	new Uint16Array(Face.Array()), gl.STATIC_DRAW);
}

BufferObject.FeedBuffers= function()
{ 
	BufferObject.FeedVBO();
    BufferObject.FeedIBO();
    BufferObject.Swap();
}



